package com.neu.api.config;

import com.neu.api.Interceptor.AppLoginInterceptor;
import com.neu.api.Interceptor.LoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.FormContentFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import javax.annotation.Resource;

@Configuration
public class MyWebConfig extends WebMvcConfigurationSupport {

    @Resource
    LoginInterceptor loginInterceptor;

    @Resource
    AppLoginInterceptor appLoginInterceptor;

    @Bean
    public FormContentFilter createFormContentFilter(){
        return new FormContentFilter();
    }

    @Bean
    public Docket createDocket() {
        ApiInfo apiInfo = new ApiInfoBuilder()
            .title("饿了么项目接口文档")
            .version("1.0")
            .description("饿了么项目接口文档")
            .build();

        Docket docket = new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo)
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.neu.api.controller"))
            .paths(PathSelectors.any())
            .build();
        return docket;
    } 

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /**
     * 跨域问题
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        /*
         * addMapping：配置可以被跨域的路径，可以任意配置，可以具体到直接请求路径。
         * allowCredentials：是否开启Cookie
         * allowedMethods：允许的请求方式，如：POST、GET、PUT、DELETE等。
         * allowedOrigins：允许访问的url，可以固定单条或者多条内容
         * allowedHeaders：允许的请求header，可以自定义设置任意请求头信息。
         * maxAge：配置预检请求的有效时间
         */
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:8080","http://127.0.0.1:8080,","http://localhost:5173","http://127.0.0.1:5173")
                .allowCredentials(true)  //cookie
                .allowedMethods("*")
                .allowedHeaders("*")
                .maxAge(3600);  //如果每次请求都加载了token, options
    }

    /**
     * 注册拦截器
     * @param registry
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor).addPathPatterns("/admin/**").excludePathPatterns("/admin/user/login");
        registry.addInterceptor(appLoginInterceptor).addPathPatterns("/app/**").excludePathPatterns("/app/customer/login","/app/customer/register");
    }
}